// Constructs an income panel for couples with DV report 
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear
*merge information about the parlamentary elections using the municipality in the year of cohabitation (note, year is the year_start_cohab)
rename kunta0 kunta
*MERGE VOTES SHARES DATA (FILL YEARS WITH NO ELECTIONS WITH THE PREVIOUS ELECTION RESULTS)
forvalues j= 2000(4)2012 {
sort kunta 
merge kunta using $data_folder\voteshares_local_mun_simple_`j', nokeep
tab _merge
drop _merge
}


gen ultracons_kunt=ultracons_kunt2004 
gen cons_kunt=cons_kunt2004 
gen rightwing_kunt=rightwing_kunt2004 
sum ultracons* cons* rightwing*

forvalues j= 2004(4)2008 {
local h=`j' +4
replace ultracons_kunt=ultracons_kunt`h' if year>=`j' & ultracons_kunt`h'!=.
replace cons_kunt=cons_kunt`h' if year>=`j' & cons_kunt`h'!=.
replace rightwing_kunt=rightwing_kunt`h' if year>=`j' & rightwing_kunt`h'!=.
}



bysort year: egen medianucons=median(ultracons_kunt)
gen conservative=ultracons_kunt>medianucons
tab conservative

bysort year: egen mediancons=median(cons_kunt)
gen conservative2=cons_kunt>mediancons
tab conservative2

bysort year: egen medianright=median(rightwing_kunt)
gen conservative3=rightwing_kunt>medianright
tab conservative3

egen quartile_extremeright=xtile(rightwing_kunt), by(year) nq(4)

gen veryconservative=quartile_extremeright==4


rename kunta kunta0

************************



local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummies used in the event studies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"


/* Own Characteristics Heterogeneity by LOCATION VOTES SHARES*/ 
lab var treatPost "Abusive x Cohabitation"
						
reghdfe ptoim1 treatPost treat if conservative3 ==1,  absorb($fe)  cluster($cluster)
	est sto ptoim1_1
reghdfe ptoim1 treatPost treat if conservative3 ==0,  absorb($fe)  cluster($cluster)
	est sto ptoim1_0
	

	coefplot (ptoim1_0, label("Conservative") bcolor(red)) ///
				 (ptoim1_1, label("Liberal") bcolor(blue)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")
			graph export "$result_folder\victim_match_het_conservative3_ptoim1.pdf", replace
			
			
reghdfe ptoim1 treatPost treat if veryconservative ==1,  absorb($fe)  cluster($cluster)
	est sto ptoim1_1
reghdfe ptoim1 treatPost treat if veryconservative ==0,  absorb($fe)  cluster($cluster)
	est sto ptoim1_0
	

	coefplot (ptoim1_0, label("Conservative") bcolor(red)) ///
				 (ptoim1_1, label("Liberal") bcolor(blue)), ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")
			graph export "$result_folder\victim_match_het_veryconservative_ptoim1.pdf", replace
			
